<template>
  <div class="app-container">
    <!-- <div class="customContainer towSearchHeader"> -->
    <div class="customContainer">
      <!-- 搜索头 -->
      <el-form :inline="true" :model="formInline" class="demo-form-inline" size="small">
        <el-form-item>
          <el-date-picker style="width:130px" v-model="pickerStart" value-format="yyyy-MM" type="month" format="yyyy-MM" :picker-options="pickerOptions"
            placeholder="开始月份"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-date-picker style="width:130px" v-model="pickerEnd" value-format="yyyy-MM" type="month" format="yyyy-MM" :picker-options="pickerOptions"
            placeholder="结束月份"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <span style="color: #888;">对接人：</span>
          <el-select v-model="formInline.emp_id" placeholder="请选择对接人" style="width:130px" clearable>
            <el-option v-for="item in docking_list" :key="item.id" :label="item.nick" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="supplier_type" style="width:130px" @change="searchChange">
            <el-option v-for="item in search_list" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
          <el-autocomplete clearable v-model="formInline.supplier" style="width:200px" :fetch-suggestions="querySearch" :placeholder="searchPld"
            :trigger-on-focus="false" valueKey="name" size="small" @keyup.enter.native="searchClick"></el-autocomplete>
        </el-form-item>
        <el-form-item>
          <dj-button size="small" @search="searchClick" />
        </el-form-item>
        <el-form-item>
          <el-tooltip class="item" effect="dark" placement="bottom-end">
            <div slot="content" style="line-height:20px;width:400px">
              订单金额、调整金额、到货金额、已付金额”全为0时，数据不展示
            </div>
            <i class="el-icon-question"></i>
          </el-tooltip>
        </el-form-item>
      </el-form>
      <!-- 表格 -->
      <el-table highlight-current-row :data="list" v-loading.body="listLoading" border size="small">
        <el-table-column align="center" label="序号" width="50">
          <template slot-scope="scope">
            <span class="indexColor">{{scope.$index+1+(currentPage-1)*20}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="供应商编号" prop="serial_number" width="100"></el-table-column>
        <el-table-column align="center" label="供应商名称" prop="name"></el-table-column>
        <el-table-column align="center" label="总部对接人" prop="butt_man"></el-table-column>
        <el-table-column align="center" label="付款类型">
          <template slot-scope="scope">
            <span>{{scope.row.pay_type|payTypeName}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="订单金额" prop="order_money" width="100"></el-table-column>
        <el-table-column align="center" label="调整金额" prop="adjust_money" width="100"></el-table-column>
        <el-table-column align="center" label="到货金额" prop="arrive_money" width="100"></el-table-column>
        <el-table-column align="center" label="已付金额" prop="pay_money" width="100"></el-table-column>
        <!-- 列表的操作按钮 -->
        <el-table-column align="center" label="操作" width="100">
          <template slot-scope="scope">
            <el-button @click="showInfo(scope.row)" type="primary" plain size="mini">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 详情弹窗 -->
    <el-dialog class="towTabel" :close-on-click-modal="false" width="800px" title="供应商货款对比详情" :visible.sync="showDialog">
      <div class="info">
        <div class="info_item tag"><span>供应商名称：</span>
          <div class="name">{{info.name}}</div>
        </div>
        <div class="info_item">供应商编号：{{info.no}}</div>
        <div class="info_item">总部对接人：{{info.butt_man}}</div>
      </div>
      <el-table highlight-current-row :data="info_list" border size="small">
        <el-table-column align="center" label="序号" width="50">
          <template slot-scope="scope">
            <span class="indexColor">{{scope.$index+1}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="月份" prop="time"></el-table-column>
        <el-table-column align="center" label="订单金额" prop="order_money"></el-table-column>
        <el-table-column align="center" label="调整金额" prop="adjust_money"></el-table-column>
        <el-table-column align="center" label="到货金额" prop="arrive_money"></el-table-column>
        <el-table-column align="center" label="已付金额" prop="pay_money"></el-table-column>
      </el-table>
    </el-dialog>
    <!-- 自定义底部 -->
    <div class="bottom_box">
      <el-pagination class="pull-left" background @current-change="handleCurrentChange" :current-page.sync="currentPage" :page-size="20"
        layout="prev, pager, next, jumper" :total="totalDataNumber">
      </el-pagination>
      <p class="pull-right" style="margin-top:10px;font-size:15px;">
        <span class="mR"> 订单金额合计:{{(order_money_total||0)|keepTwoNum}}元</span>
        <span class="mR"> 调整金额合计:{{(adjust_money_total||0)|keepTwoNum}}元</span>
        <span class="mR"> 到货金额合计:{{(arrive_money_total||0)|keepTwoNum}}元</span>
        <span> 已付金额合计:{{(pay_money_total||0)|keepTwoNum}}元</span>
      </p>
    </div>
  </div>
</template>
<script>
import { getPage, getList, getInfo } from '@/api/report/supMContForm';
import { getDocking, getSup } from '@/api/common/commonApi';
import { pickerOptionsMonthFive } from '@/utils';
export default {
  data() {
    return {
      order_money_total: 0,
      adjust_money_total: 0,
      arrive_money_total: 0,
      pay_money_total: 0,
      list: null,

      listLoading: false,
      showDialog: false,
      search_list: [
        { id: '1', name: '按供应商名称' },
        { id: '2', name: '按供应商编号' }
      ],
      docking_list: [],
      supplier_list: [], //供应商名称列表
      info_list: [], //详情列表
      info: { name: '', no: '', butt_man: '' }, //详情信息
      searchPld: '请输入供应商名称',
      supplier_type: '1',
      formInline: {
        start_time: '',
        end_time: '',
        emp_id: '',
        supplier_type: '',
        supplier: ''
      },
      totalDataNumber: 1,
      currentPage: 1,
      pickerStart: new Date(Date.UTC(new Date().getFullYear(), new Date().getMonth())).toISOString().slice(0, 7),
      //  - 1 >= 0 ? new Date().getMonth() - 1 : 0
      // new Date(Date.UTC(new Date().getFullYear(), 0)).toISOString().slice(0, 7),
      pickerEnd: new Date(Date.UTC(new Date().getFullYear(), new Date().getMonth())).toISOString().slice(0, 7),
      pickerOptions: pickerOptionsMonthFive()
    };
  },
  beforeRouteEnter(to, from, next) {
    next(vm => {
      if (!vm.list) {
        vm.getUserList();
      }
    });
  },
  methods: {
    searchChange(val) {
      this.formInline.supplier = '';
      this.searchPld = val == 1 ? '请输入供应商名称' : '请输入供应商编号';
    },
    getUserList() {
      getDocking({}).then(res => {
        this.docking_list = res;
        this.searchClick();
        this.getSupName();
      });
    },
    //获取供应商名称数组
    getSupName() {
      getSup({}).then(res => {
        this.supplier_list = res;
      });
    },
    querySearch(queryString, cb) {
      var supplier_list = this.supplier_list;
      var results = queryString ? supplier_list.filter(this.createFilter(queryString)) : supplier_list;
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    createFilter(queryString) {
      return restaurant => {
        if (this.supplier_type == 1) {
          return restaurant.name.indexOf(queryString) >= 0;
        } else {
          return false;
        }
      };
    },
    showInfo(row) {
      this.info.name = row.name;
      this.info.no = row.serial_number;
      this.info.butt_man = row.butt_man;
      getInfo({ start_time: this.formInline.start_time, end_time: this.formInline.end_time, supplier_id: row.id }).then(res => {
        if (res.code == 1) {
          res.data.forEach(val => {
            val.order_money = (val.order_money / 100).toFixed(2);
            val.adjust_money = (val.adjust_money / 100).toFixed(2);
            val.arrive_money = (val.arrive_money / 100).toFixed(2);
            val.pay_money = (val.pay_money / 100).toFixed(2);
          });
          this.info_list = res.data;
          this.showDialog = true;
        }
      });
    },
    // 搜索
    searchClick() {
      if (!this.pickerStart || !this.pickerEnd) {
        this.$message.error('请选择开始月份和结束月份');
        return;
      }
      if (new Date(this.pickerStart).getTime() > new Date(this.pickerEnd).getTime()) {
        this.$message.error('开始月份不可大于结束月份');
        return;
      }
      if (new Date(this.pickerEnd).getTime() - new Date(this.pickerStart).getTime() > 8.64e7 * 30 * 12) {
        this.$message.error('最长跨度为12个月');
        return;
      }
      this.formInline.start_time = this.pickerStart;
      this.formInline.end_time = this.pickerEnd;
      this.formInline.supplier_type = this.formInline.supplier ? this.supplier_type : '';
      this.fetchData();
    },
    getData(data) {
      data.forEach(val => {
        val.order_money = (val.order_money / 100).toFixed(2);
        val.adjust_money = (val.adjust_money / 100).toFixed(2);
        val.pay_money = (val.pay_money / 100).toFixed(2);
        val.arrive_money = (val.arrive_money / 100).toFixed(2);
      });
      this.list = data;
      this.listLoading = false;
    },
    // 列表获取
    fetchData() {
      this.listLoading = true;
      this.currentPage = 1;
      this.list = [];
      getPage(this.formInline)
        .then(response => {
          if (response.code == 1) {
            let total_data = response.data;
            this.order_money_total = (total_data.order_money / 100).toFixed(2);
            this.adjust_money_total = (total_data.adjust_money / 100).toFixed(2);
            this.pay_money_total = (total_data.pay_money / 100).toFixed(2);
            this.arrive_money_total = (total_data.arrive_money / 100).toFixed(2);
            this.totalDataNumber = total_data.page * 20;
            if (this.totalDataNumber <= 0) {
              this.listLoading = false;
              return;
            }
            getList(this.formInline).then(res => {
              if (res.code == 1) {
                this.getData(res.data);
              }
            });
          }
        })
        .catch(res => {
          this.listLoading = false;
        });
    },
    handleCurrentChange(val) {
      this.listLoading = true;
      this.list = [];
      getList(this.formInline, val).then(res => {
        this.getData(res.data);
        this.listLoading = false;
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.info {
  .info_item {
    display: inline-block;
    margin: 0 30px 10px 0;
    .name {
      display: inline-block;
      vertical-align: top;
      width: 310px;
    }
  }
  .tag {
    margin-right: 20px;
    width: 400px;
  }
}

.mR {
  margin-right: 15px;
}
</style>